//
//  LKDayAssetsViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/2.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKDayAssetsViewController.h"
#import "LKDayAssetsCell.h"
#import "LKPopChoiceView.h"
#import "LKHomeProductModel.h"
#import "LKDayAssetsDetailViewController.h"
@interface LKDayAssetsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *imgaesArray;
@property(nonatomic,strong)NSArray *contentArray;
@property(nonatomic,strong)LKPopChoiceView *choiceView;
@property(nonatomic,assign)NSInteger PageIndex;
@property(nonatomic,assign)NSInteger MaxResultCount;
@property(nonatomic,strong)NSMutableArray *dataListArray;
@property(nonatomic,assign)NSInteger count;
@end

@implementation LKDayAssetsViewController

-(LKBaseTableview *)tableview{
    if (!_tableview) {
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped registercellClasss:@[@"LKDayAssetsCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 500;
    }
    return _tableview;
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    self.PageIndex = 1;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    [self getDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}

/**
 尾部上拉加载更多
 */
-(void)footerReloadData{
    self.PageIndex++;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    [self getDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.PageIndex = 1;
    self.MaxResultCount = 10;
    self.count = 0;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableview.mj_header beginRefreshing];
    [self getDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
    // Do any additional setup after loading the view.
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_bgView removeFromSuperview];
    [_choiceView removeFromSuperview];
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(0, 0, 40, 40);
//    [btn setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        NSLog(@"筛选");
//    }];
    
    LKBaseButton *titleBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 150, 40) type:UIButtonTypeCustom title:@"产品信息" titleColor:COLOR_HEX(0x000000) layercorne:0 font:[UIFont systemFontOfSize:17] backgroudColor:[UIColor whiteColor] AndActionBlock:^(UIButton *btn) {
//        [self showChoiceView:btn];
    }];
    self.navigationItem.titleView = titleBtn;
}
-(void)showChoiceView:(UIButton *)btn{
    if (!_bgView) {
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT)];
        self.bgView.alpha = 0.5f;
        self.bgView.backgroundColor = [UIColor blackColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removebackView:)];
        [self.bgView addGestureRecognizer:tap];
    }
    if (!_choiceView) {
        self.choiceView = [[LKPopChoiceView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
        self.choiceView.valueBlock = ^(NSString *title) {
            [btn setTitle:title forState:UIControlStateNormal];
        };
        self.choiceView.titleArray = @[@"全部",@"普惠",@"基金"];
        [[UIApplication sharedApplication].keyWindow addSubview:self.choiceView];
        [UIView animateWithDuration:0.5 animations:^{
            _choiceView.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT,WIDTH ,150);
        }];
    }
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];
    [[UIApplication sharedApplication].keyWindow addSubview:self.choiceView];
    [UIView animateWithDuration:0.5 animations:^{
        _choiceView.frame = CGRectMake(0,NAVIGATION_BAR_HEIGHT,WIDTH ,150);
    }];
    
}
-(void)removebackView:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.5 animations:^{
        UIView *view = tap.view;
        [view removeFromSuperview];
        [_choiceView removeFromSuperview];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataListArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKDayAssetsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKDayAssetsCell"];
    cell.homeProductModel = self.dataListArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150.f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 22.f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LKWebViewController *vc = [[LKWebViewController alloc] init];
    LKHomeProductModel *model = self.dataListArray[indexPath.section];
    vc.url = [NSString stringWithFormat:@"%@%@",ServerlUrl,model.projectInfoDetailImg];
//    vc.homeProductModel = self.dataListArray[indexPath.section];
    vc.showCustomeTitle = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"易收益",@"交保付",@"基金"];
    }
    return _titleArray;
}
-(NSArray *)contentArray{
    if (!_contentArray) {
        _contentArray = @[@"项目名称：",@"项目名称：",@"项目名称："];
    }
    return _contentArray;
}
-(NSArray *)imgaesArray{
    if (!_imgaesArray) {
        _imgaesArray = @[@"quanbuzichan01",@"quanbuzichan02",@"quanbuzichan03"];
    }
    return _imgaesArray;
}
/**
 获取订单列表
 */
-(void)getDataListWithParam:(NSDictionary *)param{
    
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:homeProductInformation header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        if (self.PageIndex == 1) {
            [self.dataListArray removeAllObjects];
            self.count = 0;
        }
        NSDictionary *resultDic = responseObject[@"result"];
        NSArray *dataArra = resultDic[@"items"];
        for (NSDictionary *dic in dataArra) {
            LKHomeProductModel *model = [LKHomeProductModel modelWithDictionary:dic];
            self.count ++;
            if ([model.projectInfoState integerValue] != 1) {
                continue;
            }
            [self.dataListArray addObject:model];
        }
        if (self.count == [responseObject[@"result"][@"totalCount"] integerValue]) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableview reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

-(NSMutableArray *)dataListArray{
    if (!_dataListArray) {
        _dataListArray = [[NSMutableArray alloc] init];
    }
    return _dataListArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
